"""
Author: wenbin
Create: 2024-08-19
"""

from flask_wtf import FlaskForm

from app.exception import ParameterException


class BaseForm(FlaskForm):
    """
    校验基类
    """

    def __init__(self) -> None:
        super(BaseForm, self).__init__()

    def validate_parameter(self):
        """校验参数"""
        valid = super(BaseForm, self).validate()
        if not valid:
            raise ParameterException(_format_errors(self.errors))
        return self


def _format_errors(errors: dict) -> str:
    """格式化异常提示信息"""
    messages = []
    for message in errors.values():
        messages.extend(message)
    return ", ".join(messages)
